<?php
namespace app\admin\model;

use my\Security;
use think\Db;
use think\Model;

class UserInfo extends Model
{
    protected $table="xm_user_info";
    public static function getListInfo($page, $limit, $data)
    {
        $where = [];
        //旧手机号
        if (!empty($data['old_phone'])) {
            $where['old_phone'] = Security::encrypt(rtrim($data['old_phone']));
        }
        //新手机号
        if (!empty($data['new_phone'])) {
            $where['new_phone'] = Security::encrypt(rtrim($data['new_phone']));
        }
        //申请时间
        if (!empty($data['start'])) {
            $where['phone_time'] = ['>=', strtotime($data['start'])];
        }
        if (!empty($data['end'])) {
            $where['phone_time'] = ['<=', strtotime($data['end'])];
        }
        if (!empty($data['start']) && !empty($data['end'])) {
            $where['phone_time'] = ['between', [strtotime($data['start']), strtotime($data['end'])]];
        }
        if ($data['phone_check_result'] != '') {
            $where['phone_check_result'] = $data['phone_check_result'];
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('user_phone')->where($where)->count();
        $list  = Db::name('user_phone')->where($where)->limit($start, $limit)->order('id', 'desc')->select();
        foreach ($list as $key => $value) {
            //查询用户所属角色
            $is_fr                                  = Db::name('user')->where(['uid'=>$value['uid'],'false_status'=>0])->value('is_fr');
            $list[$key]['user_type']                = !empty($is_fr) ? "顺风车主" : "用户";            //申请时间
            $list[$key]['phone_time']               = !empty($value['phone_time']) ? date("Y-m-d H:i:s", $value['phone_time']) : '';            //申请时间
            $list[$key]['phone_check_time']         = !empty($value['phone_check_time']) ? date("Y-m-d H:i:s", $value['phone_check_time']) : '';//审核开始时间
            $list[$key]['phone_check_person']       = !empty($value['phone_check_person'])?getAdminInfo($value['phone_check_person'],1):'-';      //审核人
            if($value['phone_check_result'] == 0){
                $list[$key]['checkResult']          = '审核中';
            }elseif($value['phone_check_result'] == 1){
                $list[$key]['checkResult']          = "<span style='color: red'>已拒绝</span>";
            }elseif($value['phone_check_result'] == 2){
                $list[$key]['checkResult']          = "已通过";
            }
            $list[$key]['old_phone']                = Security::decrypt($value['old_phone']);
            $list[$key]['new_phone']                = Security::decrypt($value['new_phone']);
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }
    public function lookDetail($id){
        $data                           = Db::name('user_phone a')->where(['id'=>$id])->find();
        $data['old_phone']              = Security::decrypt($data['old_phone']);
        $data['new_phone']              = Security::decrypt($data['new_phone']);
        $data['phone_time']             = !empty($data['phone_time']) ? date("Y-m-d H:i:s", $data['phone_time']) : '';            //申请时间
        $data['phone_check_time']       = !empty($data['phone_check_time']) ? date("Y-m-d H:i:s", $data['phone_check_time']) : '';//审核开始时间
        $data['phone_check_person']     = !empty($data['phone_check_person'])?getAdminInfo($data['phone_check_person'],1):'-';      //审核人
        if($data['phone_check_result'] == 0){
            $data['checkResult']        = '审核中';
        }elseif($data['phone_check_result'] == 1){
            $data['checkResult']        = "<span style='color: red'>已拒绝</span>";
        }elseif($data['phone_check_result'] == 2){
            $data['checkResult']        = "已通过";
        }
        return $data;
    }
}
